#include "shield.h"

Shield::Shield()
{
	displayWidth = Iw2DGetSurfaceWidth();
	displayHeight = Iw2DGetSurfaceHeight();

	timeUnSpawned = 0;
	onStage = false;
	readyToSpawn = false;

	position = Vector2(-1000, -1000);

	SetupImages();

	boundingBox = BoundingBox(position, width, height);
}

Shield::~Shield()
{
	CleanupImages();
}

void Shield::SetupImages()
{
	shieldSprite = Iw2DCreateImageResource("shield");
	width  = shieldSprite->GetWidth();
    height = shieldSprite->GetHeight();
	widthHalf = width / 2;
	heightHalf = height / 2;
}

void Shield::CleanupImages()
{
	if(shieldSprite != NULL)
	{
		delete shieldSprite;
		shieldSprite = NULL;
	}
}

void Shield::Render()
{
	Iw2DDrawImage(shieldSprite, CIwSVec2((int16)position.x - widthHalf, (int16)position.y - heightHalf));
}

void Shield::Update(int deltaTimeMs)
{
	timeUnSpawned += deltaTimeMs;

	if(onStage == false  && timeUnSpawned > SHIELD_TIME)
	{
		float random = Random::Instance()->RandomFloatZeroToOne();
		if(random * ((timeUnSpawned - SHIELD_TIME)/1000) > 5)
		{
			readyToSpawn = true;
		}
	}
}

void Shield::Spawn()
{
	position = Vector2(Random::Instance()->RandomIntBetween(0, displayWidth), Random::Instance()->RandomIntBetween(0, displayHeight));
	boundingBox.UpdatePosition(position);

	onStage = true;
	readyToSpawn = false;
}

void Shield::UnSpawn()
{
	position = Vector2(-1000, -1000);
	boundingBox.UpdatePosition(position);

	timeUnSpawned = 0;
	onStage = false;
}